/**
 * 
 */
package com.podmanager.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Tony
 *
 */
public class PMDatabase extends SQLiteOpenHelper {
	private static final String TAG = "PODMANAGERDATABASE";
	private static final String DATABASE_NAME = "PODMANAGER";
	private static final int DATABASE_VERSION = 1;
	private static final String[] CREATE_DATABASE = new String[]{
												SUBSCRIPTIONS.TABLE_CREATE, 
												EPISODES.TABLE_CREATE, 
												PLAYLIST.TABLE_CREATE,
												SETTINGS.TABLE_CREATE};

	public PMDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			for(String sql : CREATE_DATABASE){
			    db.execSQL(sql);
			}
		} catch (Exception ex) {
			Log.w(TAG, ex.getMessage().toString());
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database. Existing contents will be lost. ["
	            + oldVersion + "]->[" + newVersion + "]");
		db.execSQL("DROP TABLE IF EXISTS "+SUBSCRIPTIONS.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "+EPISODES.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "+PLAYLIST.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "+SETTINGS.TABLE_NAME);
		onCreate(db);
	}
	
	public static final class SUBSCRIPTIONS
	{
		public static final String 	
				TABLE_NAME 			= "SUBSCRIPTIONS",
				KEY_ROW_ID			= "_id",
				SUB_TITLE			= "SUB_TITLE",
				URL_FEED			= "URL_FEED",
				SUB_UPDATE_DT		= "SUB_UPDATE_DT",
				SUB_DESCRIPTION		= "SUB_DESCRIPTION",
				EPISODE_COUNT		= "EPISODE_COUNT",
				EPISODES_DOWNLOADED	= "EPISODES_DOWNLOADED",
				SUB_IMAGE_URL		= "SUB_IMAGE_URL",
				SUB_IMAGE_PATH		= "SUB_IMAGE_PATH";
		static final String
				TABLE_CREATE		= 			
					"create table " + TABLE_NAME +
					" (" + 
					KEY_ROW_ID + " integer primary key autoincrement not null, " +
					SUB_TITLE			+ " text, "+
					URL_FEED			+ " text not null, "+
					SUB_UPDATE_DT		+ " text, "+
					SUB_DESCRIPTION		+ " text, "+
					EPISODE_COUNT		+ " integer,"+
					EPISODES_DOWNLOADED	+ " integer,"+
					SUB_IMAGE_URL		+ " text, "+
					SUB_IMAGE_PATH		+ " text"+
					")";
		public static final String[]  COLUMN_NAMES = {
			KEY_ROW_ID,
			SUB_TITLE,
			URL_FEED,
			SUB_UPDATE_DT,
			SUB_DESCRIPTION,
			EPISODE_COUNT,
			EPISODES_DOWNLOADED,
			SUB_IMAGE_URL,
			SUB_IMAGE_PATH};
	}
	public static final class EPISODES
	{
		public static final String
				TABLE_NAME 			= "EPISODES",
				KEY_ROW_ID			= "_id",
				SUB_ROW_ID			= "SUB_ROWID",
				EPI_TITLE			= "EPI_TITLE",
				FILE_URL			= "FILE_URL",
				FILE_PATH			= "FILE_PATH",
				EPI_DATE			= "EPI_DATE",
				EPI_DESCRIPTION		= "EPI_DESCRIPTION",
				EPI_IMAGE_URL		= "EPI_IMAGE_URL",
				EPI_IMAGE_PATH		= "EPI_IMAGE_PATH",
				DURATION			= "DURATION",
				POSITION			= "POSITION",
				IS_READ				= "IS_READ",
				IS_IN_PLAYLIST		= "IS_IN_PLAYLIST";
		static final String 
				TABLE_CREATE		= 		
					"create table " + TABLE_NAME +
					" (" + 
					KEY_ROW_ID + " integer primary key autoincrement not null, " +
					SUB_ROW_ID			+ " integer not null, "+
					EPI_TITLE			+ " text not null, "+
					FILE_URL			+ " text not null, "+
					FILE_PATH			+ " text, "+
					EPI_DATE			+ " text, "+
					EPI_DESCRIPTION		+ " text, "+
					EPI_IMAGE_URL		+ " text, "+
					EPI_IMAGE_PATH		+ " text, "+
					DURATION			+ " integer not null, "+
					POSITION			+ " integer not null, "+
					IS_READ				+ " integer not null, "+
					IS_IN_PLAYLIST		+ " integer not null"+
					")";
		public static final String[]  COLUMN_NAMES = {
			KEY_ROW_ID,
			SUB_ROW_ID,
			EPI_TITLE,
			FILE_URL,
			FILE_PATH,
			EPI_DATE,
			EPI_DESCRIPTION,
			EPI_IMAGE_URL,
			EPI_IMAGE_PATH,
			DURATION,
			POSITION,
			IS_READ,
			IS_IN_PLAYLIST
		};
	}
	public static final class PLAYLIST
	{
		public static final String
				TABLE_NAME 			= "PLAYLIST",
				KEY_ROW_ID			= "_id",
				EPI_ROW_ID			= "EPI_ROWID";
		
		static final String 
				TABLE_CREATE		= 		
					"create table " + TABLE_NAME +
					" (" + 
					KEY_ROW_ID + " integer primary key autoincrement not null, " +
					EPI_ROW_ID			+ "integer not null"+
					")";
		public static final String[]  COLUMN_NAMES = {
			KEY_ROW_ID,
			EPI_ROW_ID
		};
	}
	
	public static final class SETTINGS 
	{
		public static final String
			TABLE_NAME 	= "SETTINGS",
			KEY_ROW		= "KEY",
			VALUE_ROW 	= "VALUE";
		
		static final String
			TABLE_CREATE =
				"create table " + TABLE_NAME +
				" (" +
				KEY_ROW + " text, " +
				VALUE_ROW + " text" +
				")";
		public static final String[]  COLUMN_NAMES = {
			KEY_ROW,
			VALUE_ROW
		};
	}
	
	
}
